package com.base;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.net.URL;
import java.util.Properties;

/**
 * @author gaoxing
 * @create 2018-09-17 下午2:33
 */
public class BasePropertiesUtils {
    private static final Logger log = LoggerFactory.getLogger(BasePropertiesUtils.class);

    public BasePropertiesUtils() {
    }

    public static Properties load(String fileName) {
        Properties props = null;

        try {
            URL url = BasePropertiesUtils.class.getClassLoader().getResource(fileName);
            if (url != null) {
                props = PropertiesLoaderUtils.loadProperties(new EncodedResource(new UrlResource(url), "utf-8"));
            }
        } catch (Throwable var3) {
            log.error("加载配置文件[{}]异常: {}", fileName, ExceptionUtils.getStackTrace(var3));
        }

        if (props == null) {
            log.error("找不到配置文件:" + fileName);
        }

        return props;
    }
}
